// ===================== 面向初学者的详细注释 =====================
// frmdeviceupdate.h 声明了 frmdeviceupdate 类，用于实现设备固件升级、主题配置等界面与逻辑。
// 包括进入/退出指令模式、下发/读取/保存/取消配置、超时处理等。
// 每个成员变量和函数都加了详细注释，便于初学者理解。
// ==============================================================

#ifndef FRMDEVICEUPDATE_H
#define FRMDEVICEUPDATE_H

#include <QWidget>
#include <QTimer>
#include "frmmain.h"
#include "config.h"
#include "ui_frmdeviceupdate.h"

namespace Ui {
class frmdeviceupdate;
}

class frmdeviceupdate : public QWidget
{
    Q_OBJECT

public:
    explicit frmdeviceupdate(QWidget *parent = nullptr);
    ~frmdeviceupdate();

private slots:
    void on_enterCommandModeButton_clicked();
    void on_exitCommandModeButton_clicked();
    void on_sendConfigButton_clicked();
    void on_cancelButton_clicked();
    void on_saveConfigButton_clicked();
    void on_loadConfigButton_clicked();
    void onSerialConnectionChanged(bool connected);
    void onTcpConnectionChanged(bool connected);
    void onSerialDataReceived(const QString &data);
    void onTcpDataReceived(const QString &data);
    void onResponseTimeout();
    void on_btnReportTopic_clicked();
    void on_btnDownlinkTopic_clicked();
    void on_btnResponseTopic_clicked();
    void on_queryTopicTypeComboBox_currentIndexChanged(int index);
    void onWaitLogTimeout();

private:
    Ui::frmdeviceupdate *ui;
    frmmain *mainWindow;
    bool isCommandMode;
    Config config;
    bool isWaitingForResponse;
    QString lastCommand;
    QTimer responseTimer;
    bool isWaitingForMqttConfig;
    bool isProcessingTopicQuery;
    bool isWaitingForTopicResponse;
    QTimer waitLogTimer;
    int waitSeconds;

    void sendCommandModeCommand(bool enter);
    void updateMainResponse(const QString &msg);
    void enableConfigControls(bool enable);
    void resetConfigFields();
    void updateSettingModeButtonState();
    void startResponseTimer();
    void sendMqttQueryCommand();
    void parseMqttConfigResponse(const QString &data);
    void parseTopicResponse(const QString &data);
    void parseSaveConfigResponse(const QString &data);
    void sendTopicCommand(const QString &command, const QString &desc);
    bool eventFilter(QObject *watched, QEvent *event) override;
};

#endif // FRMDEVICEUPDATE_H